package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.PublishPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.PublishView
import com.weetop.cfw.bean.*
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.home_page.activity.PublishActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class PublishPresenterImp(var publishView: PublishView? = null): PublishPresenter {

    /**
     * 获取所有站点数据
     */
    override fun getAllSitesData(publishActivity: PublishActivity) {
       RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getAllSitesData(),publishActivity,object:RxJavaCallBack<AllSitesBean>(){
           override fun onSuccess(t: AllSitesBean?) {
               publishView?.getAllSitesDataSuccess(t)
           }
       })
    }

    /**
     * 获取建筑类型
     */
    override fun getBuildType(publishActivity: PublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().kind(),publishActivity,object:RxJavaCallBack<KindBean>(){
            override fun onSuccess(t: KindBean?) {
                publishView?.getBuildKindDataSuccess(t)
            }
        })
    }

    /**
     * 发布出租出售信息
     */
    override fun publishLeaseSaleInformation(publishActivity: PublishActivity, publishMap: HashMap<String, String>) {
        publishActivity.showLoadingDialog(publishActivity,"正在发布，请稍等...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().publishLeaseSaleInformation(publishMap),publishActivity,object:RxJavaCallBack<PublishBean>(){
            override fun onSuccess(t: PublishBean?) {
                publishActivity.dismissLoadingDialog()
                publishView?.leaseSaleInformationPublishSuccess(t)
//                ActivityUtils.finishActivity(publishActivity)
            }

            override fun onRequestError(t: PublishBean?) {
                super.onRequestError(t)
                publishActivity.dismissLoadingDialog()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                publishActivity.dismissLoadingDialog()
            }
        })
    }





    /**
     * 行业数据
     */
    override fun getIndustryList(publishActivity: PublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1700),publishActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                if(t != null){
                    publishView?.industryDataGetSuccess(t)
                }
            }

            override fun onRequestError(t: GeneralCodeTableBean?) {
                super.onRequestError(t)
                if(t != null){
                    publishActivity.showNativeShortToast(t.message)
                }
            }

        })
    }

    /**
     * 面积归类数据
     */
    override fun areaClassificationList(publishActivity: PublishActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1100),publishActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                if(t != null){
                    publishView?.areaClassificationDataGetSuccess(t)
                }
            }

            override fun onRequestError(t: GeneralCodeTableBean?) {
                super.onRequestError(t)
                if(t != null){
                    publishActivity.showNativeShortToast(t.message)
                }
            }

        })
    }

    /**
     * 根据选择的站点获取下面对应的区县
     */
    override fun getDistrictAndCountyData(publishActivity: PublishActivity, cityId: Int) {
       RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().quYu(cityId),publishActivity,object:RxJavaCallBack<SiteBean>(){
           override fun onSuccess(t: SiteBean?) {
                  publishView?.getDistrictAndCountyDataSuccess(t)
           }
       })
    }

    /**
     * 获取当前城市对应的站点数据
     * @param cityName 城市名称
     */
    override fun getSiteInfoByCity(publishActivity: PublishActivity,cityName: String) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getCityIdByCityName(cityName),publishActivity,object:RxJavaCallBack<CityIdBean>(){
            override fun onSuccess(t: CityIdBean?) {
                publishView?.siteInfoGetSuccess(t)
            }

        })
    }

    /**
     * 根据站点ID获取出租价格单位
     */
    override fun getPriceUnitBySiteId(publishActivity: PublishActivity, siteId: Int) {
        publishActivity.showLoadingDialog(publishActivity,"正在获取价格单位...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getPriceUnitBtSite(siteId),publishActivity,object:RxJavaCallBack<PriceUnitBean>(){
            override fun onSuccess(t: PriceUnitBean?) {
                publishActivity.dismissLoadingDialog()
                publishView?.getPriceUnitSuccess(t?.punit)
            }

            override fun onRequestError(t: PriceUnitBean?) {
                super.onRequestError(t)
                publishActivity.dismissLoadingDialog()
                publishView?.showNativeShortToast(t?.message)
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                publishActivity.dismissLoadingDialog()
            }

        })
    }

    /**
     * 上传图片
     */
    override fun uploadWorkshopDetailImage(publishActivity: PublishActivity, newHeadImgFile: String) {
        publishActivity.showLoadingDialog(publishActivity,"正在上传图片...")
        val file = File(newHeadImgFile)
        val image: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val headImgPart = MultipartBody.Part.createFormData("Imgfile", file.name, image)
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().uploadImage(multipartFile = headImgPart),publishActivity,object:RxJavaCallBack<UploadImageBean>(){
            override fun onSuccess(t: UploadImageBean?) {
                if(t != null){
                    publishActivity.dismissLoadingDialog()
                    publishView?.uploadImageSuccess(t.imgurl)
                }
            }

            override fun onRequestError(t: UploadImageBean?) {
                super.onRequestError(t)
                publishActivity.dismissLoadingDialog()
                publishView?.showNativeShortToast(t?.message)
                publishView?.uploadImageError()

            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                publishActivity.dismissLoadingDialog()
                publishView?.uploadImageError()
            }

        })
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        publishView = baseView as? PublishView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        publishView = null
    }
}